package com.qf;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.converters.Auto;
import com.qf.feign.SearchFeign;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.naming.directory.SearchResult;

/**
 * @author sunyamei
 * @version V1.0
 * @Project javase2203
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/8/11 10:07
 */
@RestController
@RequestMapping("/custom")
public class TestController {

    //发送远程请求的对象
    @Autowired
    private RestTemplate restTemplate;

    //eureka客户端对象
    @Autowired
    private EurekaClient eurekaClient;

    @Autowired
    private SearchFeign searchFeign;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    /**
     * 测试模拟feign底层原理, 发送http协议
     * 并且使用了ribbon负载均衡技术
     * @return
     */
    @RequestMapping("/t1")
    public String  customer(){
        //1. 通过Eureka客户端获取Search微服务的对象信息, 被调用微服务的服务名
        InstanceInfo info = eurekaClient.getNextServerFromEureka("QF-SEARCH", false);

        //2. 从搜索微服务的对象信息中获取ip地址和端口号, 也就是访问路径
        String url = info.getHomePageUrl();
        System.out.println("======search URL地址========" + url);

        //3. 发送远程调用请求(http请求), 访问被调用的微服务
        String result = restTemplate.getForObject("http://QF-SEARCH/search/test1", String.class);

        //4. 返回结果
        return result;
    }


    /**
     * 测试feign远程调用技术
     * feign底层封装了ribbon接口负载均衡技术,
     * 所以在使用feign的时候自动就使用了ribbon负载均衡
     * @return
     */
    @RequestMapping("/t2")
    public String test2() {
        String str = searchFeign.test();
        return str;
    }

    @RequestMapping("/t3")
    public String insertUser() {
        User user = new User();
        user.setId(1);
        user.setName("青龙");
        user.setSex("男");
        String str = searchFeign.insertUser(user);
        return str;
    }

    @RequestMapping("/t4")
    public String selectByName() {
        String str = searchFeign.findByName("张三");
        return str;
    }
}